package constant.milk.cashbook.sms;

import java.util.ArrayList;
import java.util.Calendar;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import constant.milk.cashbook.R;
import constant.milk.cashbook.SuperActivity;
import constant.milk.cashbook.bean.SmsData;
import constant.milk.cashbook.tip.TipInfoActivity;
import constant.milk.cashbook.tip.TipMap;
import constant.milk.cashbook.util.MilkTextView;

public class SmsInsertActivity extends SuperActivity
{
	private static final int REQUEST_SMS_INSERT = 0;
	
	private ListView listView;
	private Button addButton;
	
	private SmsAdapter adapter;
	
	private int items;
	
	private SmsData smsData;
	
	private ArrayList<SmsData> list;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.sms_insert_activity);
		
		setTitle("문자추가");
		
		setFindViewId();
		setOnClick();
		setData();
	}

	@Override
	protected void setFindViewId()
	{
		listView = (ListView) findViewById(R.id.listView);
		addButton = (Button) findViewById(R.id.addButton);
	}

	@Override
	protected void setOnClick()
	{
		addButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(superContext, SmsAssayActivity.class);
				intent.putExtra(SmsAssayActivity.SMS_ADDRESS, smsData.getAddress());
				intent.putExtra(SmsAssayActivity.SMS_MESSAGE, smsData.getMemo());
				startActivityForResult(intent, REQUEST_SMS_INSERT);
			}
		});
	}

	@Override
	protected void setData()
	{
		new ShowDialog().execute(0);
	}
	
	private OnItemClickListener onItemClickListener = new OnItemClickListener()
	{
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int position,
				long arg3)
		{
			smsData = (SmsData) arg0.getItemAtPosition(position);
			items = position;
			adapter.notifyDataSetChanged();
			
		}
	};
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		if (resultCode != Activity.RESULT_OK)
		{
			return;
		}
		
		if (requestCode == REQUEST_SMS_INSERT)
		{
			setResult(Activity.RESULT_OK);
			finish();
		}
	}

	private class SmsAdapter extends ArrayAdapter<SmsData>
	{
		public SmsAdapter(Context context, int textViewResourceId, ArrayList<SmsData> objects) 
		{
			super(context, textViewResourceId, objects);
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) 
		{
			View v = convertView;
			
			if (v == null) 
			{
				LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = inflater.inflate(R.layout.sms_list_item, null);
			}
			
			SmsData data = getItem(position);
			
			MilkTextView address = (MilkTextView) v.findViewById(R.id.smsItemAddressTextView);
			MilkTextView message = (MilkTextView) v.findViewById(R.id.smsItemMessageTextView);
			RadioButton radio = (RadioButton) v.findViewById(R.id.smsItemRadio);
			
			address.setText(data.getAddress());
			message.setText(data.getMemo());
			
			if (items == position)
			{
				radio.setChecked(true);
			}
			else
			{
				radio.setChecked(false);
			}
			
			return v;
		}
	}
	
	/**
	 * 다이얼로그 뛰우기 
	 */
	private class ShowDialog extends AsyncTask<Integer, Integer, Integer>
	{
		ProgressDialog dialog = null;
		@Override
		protected void onPreExecute() 
		{
			super.onPreExecute();
			dialog = new ProgressDialog(SmsInsertActivity.this);
			dialog.setCancelable(false);
			dialog.setMessage("불러오는중...");
			dialog.show();
		}
		
		@Override
		protected Integer doInBackground(Integer... arg0) 
		{
			if (arg0[0] == 0) 
			{
				Calendar cal1 = Calendar.getInstance();
				
				Calendar cal2 = Calendar.getInstance();
				cal2.add(Calendar.MONTH, -3);
				
				SmsGet smsGet = new SmsGet(superContext);
				list = smsGet.getSms(cal1.getTimeInMillis(), cal2.getTimeInMillis());
				
				return 0;
			}
			
			return 0;
		}

		@Override
		protected void onPostExecute(Integer result) 
		{
			super.onPostExecute(result);

			if (dialog != null) 
			{
				dialog.dismiss();
			}
			
			if (result == 0)
			{
				if (list.size() > 0)
				{
					smsData = list.get(0);
					items = 0;
				}
				
				listView.setOnItemClickListener(onItemClickListener);
				
				adapter = new SmsAdapter(
						superContext, R.layout.sms_list_item, list);
				listView.setAdapter(adapter);
			}
		}
	}
	
	@Override
	protected void tipButton()
	{
		Intent intent = new Intent(superContext, TipInfoActivity.class);
		intent.putExtra(TipInfoActivity.TIP_ID, TipMap.TIP_AUTO_SMS_INSERT);
		startActivity(intent);
	}
}
